package com.exam.service.impl;

import com.exam.dao.ExamDao;
import com.exam.dao.QuestionDao;
import com.exam.domain.entity.Exam;
import com.exam.domain.entity.Question;
import com.exam.service.QuestionService;
import com.exam.util.BaseResult;

import java.util.List;

public class QuestionServiceImpl implements QuestionService {
    private final QuestionDao questionDao;
    private final ExamDao examDao;

    public QuestionServiceImpl() {
        questionDao = new QuestionDao();
        examDao = new ExamDao();
    }

    @Override
    public BaseResult<Question> getQuestionDetailsById(Integer questionId) {
        Question question = questionDao.getQuestionById(questionId);

        return BaseResult.success(question);
    }

    @Override
    public BaseResult<String> updateQuestion(Integer questionId,String questionText, String correctOption, String optionA, String optionB, String optionC, String optionD) {
        Integer result = questionDao.updateQuestion(questionId,questionText,correctOption,optionA,optionB,optionC,optionD);
        return result>0?BaseResult.success("更新成功"):BaseResult.error("更新失败");
    }

    @Override
    public BaseResult<String> deleteQuestionById(Integer questionId) {
        // 删除之前查询是否有试卷关联了这个问题
        List<Exam> examList = examDao.relatedExamListByQuestionId(questionId);
        if(!examList.isEmpty()){
            return BaseResult.error("您发布的试卷："+examList.get(0).getTitle()+"，关联了该问题，请勿删除");
        }
        Integer result = questionDao.deleteQuestionById(questionId);
        return result>0?BaseResult.success("删除成功"):BaseResult.error("删除失败");
    }
}
